//author:kakake
var Model = /** @class */ (function () {
    function Model() {
        this.Books = {
            "model": {
                "Id": "",
                "BookName": "",
                "BuyPrice": "",
                "BuyDate": "yyyy-MM-dd",
                //"language": "0",
                "memo": "",
                "Flag": 0,
                "WorkId": 0
            },
            "tablename": "Books",
            "key": "Id",
            "val": "0",
            "objval": { "Id": 0, "BookName": "" },
            "query": {
                "BookName": "like"
            },
            "where": "",
            "pageinfo": {},
            "WorkId": 1
        };
    }
    return Model;
}());

//author:kakake
var Common = /** @class */ (function () {
    function Common() {
    }
    Common.prototype.test = function () {
    };
    return Common;
}());

//author:kakake
var tsbookController = /** @class */ (function () {
    function tsbookController() {
        this.dbConn = TSLobsterAPI.Program.GetDbConn();
        this.bookModel = new Model().Books;
    }
    tsbookController.prototype.getdata = function () {
        var pageInfo = {
            key: "Id",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        this.bookModel.where = TSLobsterAPI.DbHelper.GetWhere(this.bookModel.query);
        this.bookModel.pageinfo = pageInfo;
        TSLobsterAPI.Utils.TrackLog("where", this.bookModel.where);
        TSLobsterAPI.Utils.TrackLog("WorkId", this.bookModel.WorkId);
        var job = TSLobsterAPI.AppFunction.GetCustomFactory("Lobster.Quartz.IJobService").GetActiveJob();
        var datagrid = { data: {}, count: 0, job: job, date: TSLobsterAPI.Utils.dateFormat(new Date()) };
        datagrid.data = TSLobsterAPI.DbHelper.GetListModelPage(this.dbConn, this.bookModel);
        datagrid.count = pageInfo.totalRecord;
        //TSLobsterAPI.Utils.TrackLog("datagrid",datagrid);
        return datagrid;
    };
    tsbookController.prototype.testsys = function () {
        var workid = TSLobsterAPI.Program.GetWorkId();
        var appid = TSLobsterAPI.Program.GetAppId();
        var ret = TSLobsterAPI.AppFunction.GetSystemConfigs(workid, appid, 'SubMchId');
    };
    tsbookController.prototype.getdata2 = function () {
        var pageInfo = {
            key: "Id",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        //var strsql="select * from Books where flag=? and workid=?";
        var strsql = "select * from Books";
        var param = null;
        // param.push(0);
        // param.push(TSLobsterAPI.Program.GetWorkId());
        TSLobsterAPI.Utils.TrackLog("strsql", strsql);
        TSLobsterAPI.Utils.TrackLog("WorkId", param);
        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.dbConn, strsql, param, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    };
    tsbookController.prototype.savemodel = function () {
        var self = this;
        self.bookModel.objval = JSON.parse(TSLobsterAPI.Program.RequestBody());
        if (self.existbookname(self.bookModel.objval.BookName) && self.bookModel.objval.Id == 0) {
            throw '请不要添加重复的书籍名称';
        }
        TSLobsterAPI.Utils.TrackLog("TableModel", self.bookModel);
        TSLobsterAPI.DbHelper.SaveModel(self.dbConn, self.bookModel);
        return { result: true };
    };
    tsbookController.prototype.deletemodel = function () {
        var self = this;
        //self.TableModel.val = TSLobsterAPI.Program.RequestQuery("Id", "0");
        //TSLobsterAPI.DbHelper.DeleteModel(self.data.dbConn, self.TableModel);
        var id = TSLobsterAPI.Program.RequestQuery("Id", "0");
        self.bookModel.val = id;
        var book = TSLobsterAPI.DbHelper.GetModel(self.dbConn, self.bookModel);
        var strsql;
        if (book.Flag == 0) {
            strsql = "update books set flag=1 where id=@id";
        }
        else {
            strsql = "update books set flag=0 where id=@id";
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(self.dbConn, strsql, { id: id });
        return { result: true };
    };
    tsbookController.prototype.existbookname = function (bookname) {
        var self = this;
        var strsql = "select count(1) ct from Books where bookname=@bookname";
        var dbparams = { bookname: bookname };
        var res = TSLobsterAPI.DbHelper.DbGetJsonObject(self.dbConn, strsql, dbparams);
        if (res.ct > 0) {
            return true;
        }
        else {
            return false;
        }
    };
    return tsbookController;
}());

